<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="门店名称" prop="storeName">
          <el-input
            v-model="queryParams.storeName"
            placeholder="请输入门店名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="门店状态" prop="storeStatus">
          <el-select v-model="queryParams.storeStatus" placeholder="请选择门店状态" clearable>
            <el-option
              v-for="dict in wms_store_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="managerName">
          <el-input
            v-model="queryParams.managerName"
            placeholder="请输入负责人姓名"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt20">

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">门店信息</span></el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['wms:storeInformation:add']"
          >新增</el-button>
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['wms:storeInformation:export']"
          >导出</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="storeInformationList" border class="mt20">
        <el-table-column label="门店编号" prop="id" v-if="true"/>
        <el-table-column label="门店名称" prop="storeName" />
        <el-table-column label="门店地址" prop="storeAddress" />
        <el-table-column label="联系电话" prop="contactPhone" />
        <el-table-column label="营业时间" prop="businessHours" />
        <el-table-column label="门店状态" prop="storeStatus">
          <template #default="scope">
            <dict-tag :options="wms_store_status" :value="scope.row.storeStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="负责人姓名" prop="managerName" />
        <el-table-column label="负责人联系电话" prop="managerContactPhone" />
        <el-table-column label="备注" prop="remark" />
        <el-table-column label="操作" align="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:storeInformation:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:storeInformation:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row>
        <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>

    </el-card>
    <!-- 添加或修改门店信息对话框 -->
    <el-drawer :title="title" v-model="open" size="50%" append-to-body>
      <el-form ref="storeInformationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门店名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入门店名称" />
        </el-form-item>
        <el-form-item label="门店地址" prop="storeAddress">
          <el-input v-model="form.storeAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="营业时间" prop="businessHours">
          <el-input v-model="form.businessHours" placeholder="请输入营业时间" />
        </el-form-item>
        <el-form-item label="门店状态" prop="storeStatus">
          <el-select v-model="form.storeStatus" placeholder="请选择门店状态">
            <el-option
              v-for="dict in wms_store_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="managerName">
          <el-input v-model="form.managerName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="负责人联系电话" prop="managerContactPhone">
          <el-input v-model="form.managerContactPhone" placeholder="请输入负责人联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="StoreInformation">
import { listStoreInformation, getStoreInformation, delStoreInformation, addStoreInformation, updateStoreInformation } from "@/api/wms/storeInformation";
const { proxy } = getCurrentInstance();
const { wms_store_status } = proxy.useDict('wms_store_status');

const storeInformationList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    storeName: undefined,
    storeStatus: undefined,
    managerName: undefined,
  },
  rules: {
    storeName: [
      { required: true, message: "门店名称不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
    storeStatus: [
      { required: true, message: "门店状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询门店信息列表 */
function getList() {
  loading.value = true;
  listStoreInformation(queryParams.value).then(response => {
    storeInformationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    storeName: null,
    storeAddress: null,
    contactPhone: null,
    businessHours: null,
    storeStatus: null,
    managerName: null,
    managerContactPhone: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("storeInformationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加门店信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStoreInformation(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改门店信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["storeInformationRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateStoreInformation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addStoreInformation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除门店信息编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delStoreInformation(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/storeInformation/export', {
    ...queryParams.value
  }, `storeInformation_${new Date().getTime()}.xlsx`)
}

getList();
</script>
